//
//  SearchVC.m
//  MyTest
//
//  Created by zhiyou on 16-4-12.
//  Copyright (c) 2016年 wangyang. All rights reserved.
//

#import "SearchVC.h"

@interface SearchVC () <UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate>
{
    CGRect _rect;
}
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic ,strong) UIButton *button;
@end

@implementation SearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    _dataArray = [[NSMutableArray alloc] init];
    [_dataArray addObject:@"搜索历史"];
    [self setBar];
    
    [self.view addSubview:self.tableView];
}
#pragma mark -- 创建tableView
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, Width, Height-64-49) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}
#pragma mark -- UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = _dataArray[indexPath.row];
    if (indexPath.row == 0) {
        cell.textLabel.textColor = [UIColor redColor];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    _rect = cell.frame;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}
#pragma mark -- 创建导航
- (void)setBar
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, 64)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:bgView.frame];
    imageView.image = [UIImage imageNamed:@"topbar"];
    [bgView addSubview:imageView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 20, 50, 40);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"F_iocn_normal"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:backBtn];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectZero;
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [cancelBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:cancelBtn];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top).offset(20);
        make.right.offset(-10);
        make.width.offset(50);
        make.height.offset(40);
    }];
    
    _textField = [[UITextField alloc] initWithFrame:CGRectZero];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.placeholder = @"搜索";
    _textField.clearsOnBeginEditing = YES;
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    _textField.delegate = self;
    [bgView addSubview:_textField];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backBtn.mas_right).offset(0);
        make.right.equalTo(cancelBtn.mas_left).offset(-10);
        make.top.equalTo(bgView.mas_top).offset(25);
        make.bottom.equalTo(bgView.mas_bottom).offset(-10);
    }];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_dataArray addObject:_textField.text];
    [_tableView reloadData];
    if (_dataArray .count > 1) {
        [_tableView addSubview:self.button];
        [self showButton];
    }
    _textField.text = nil;
    return YES;
}
- (UIButton *)button
{
    if (_button == nil) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.center = CGPointMake(self.view.center.x, _rect.origin.y+64+44);
        _button.bounds = CGRectMake(0, 0, 80, 40);
        [_button setTitle:@"清除历史" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(clearData:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}
- (void)clearData:(UIButton *)button
{
    _dataArray = [NSMutableArray arrayWithObject:@"搜索数据"];
    [_tableView reloadData];
    [button removeFromSuperview];
}
- (void)showButton
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.001];
    _button.center = CGPointMake(self.view.center.x, _rect.origin.y+64+44);
    _button.bounds = CGRectMake(0, 0, 80, 40);
    [UIView commitAnimations];
}
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
